name: Docker Image CI

on:
  pull_request:
    branches: [ "feedback" ]

jobs:

  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup Java + Maven
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '21'

      - name: Maven clean install
        run: mvn clean install

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and push the Docker Image - CONVERTER
        run: |
          docker build --build-arg JAR_FILE=target/\*.jar --tag ${{ secrets.DOCKER_USERNAME }}/hw3-converter:${GITHUB_REF_NAME//\//-}-${GITHUB_SHA::7} ./converter
          docker push ${{ secrets.DOCKER_USERNAME }}/hw3-converter:${GITHUB_REF_NAME//\//-}-${GITHUB_SHA::7}

      - name: Push latest - CONVERTER
        if: ${{ github.ref_name == 'main' }}
        run: |
          docker tag ${{ secrets.DOCKER_USERNAME }}/hw3-converter:${GITHUB_REF_NAME//\//-}-${GITHUB_SHA::7} ${{ secrets.DOCKER_USERNAME }}/hw3-converter:latest
          docker push ${{ secrets.DOCKER_USERNAME }}/hw3-converter:latest

      - name: Build and push the Docker Image - ACCOUNTS
        run: |
          docker build --build-arg JAR_FILE=target/\*.jar --tag ${{ secrets.DOCKER_USERNAME }}/hw3-accounts:${GITHUB_REF_NAME//\//-}-${GITHUB_SHA::7} ./accounts
          docker push ${{ secrets.DOCKER_USERNAME }}/hw3-accounts:${GITHUB_REF_NAME//\//-}-${GITHUB_SHA::7}

      - name: Push latest - ACCOUNTS
        if: ${{ github.ref_name == 'main' }}
        run: |
          docker tag ${{ secrets.DOCKER_USERNAME }}/hw3-accounts:${GITHUB_REF_NAME//\//-}-${GITHUB_SHA::7} ${{ secrets.DOCKER_USERNAME }}/hw3-accounts:latest
          docker push ${{ secrets.DOCKER_USERNAME }}/hw3-accounts:latest

  autotest:
    needs: build
    uses: central-university-dev/hse-ab-cicd-hw/.github/workflows/autotests-hw9.yml@main
    with:
      chart-path: ./helm-hw2
      converter-image-name: avdm2/hw3-converter
      accounts-image-name: avdm2/hw3-accounts
      image-tag: ${GITHUB_REF_NAME//\//-}-${GITHUB_SHA::7}